package com.briup.bookstore.web.controller;

import com.briup.bookstore.common.response.Result;
import com.briup.bookstore.domain.vo.BookShopCarDetailVO;
import com.briup.bookstore.service.ShopcarService;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequestMapping("/shopCar")
@RequiredArgsConstructor
public class ShopcarController {

    private final ShopcarService shopcarService;

    @GetMapping("/")
    public Result<List<BookShopCarDetailVO>> list() {
        List<BookShopCarDetailVO> list
                = shopcarService.listByUserId();
        return Result.success(list)
                .apply("查询购物车图书详情列表成功");
    }

    @PostMapping("/{bookId}/{num}")
    public Result<?> addBookToShopcar(@PathVariable Integer bookId,
                                      @PathVariable Integer num) {
        shopcarService.addBookToShopcar(bookId, num);
        return Result.success(null)
                .apply("添加图书到购物车成功");
    }

    @PutMapping("/{id}/{num}")
    public Result<?> updateShopcarNum(@PathVariable Integer id,
                                      @PathVariable Integer num) {
        shopcarService.updateShopcarNum(id, num);
        return Result.success(null)
                .apply("更新购物车图书数量成功");
    }

    @DeleteMapping("/")
    public Result<?> deleteShopcarItem(
            @RequestBody
            @NotEmpty(message = "购物车图书项ID列表不能为空") List<Integer> ids) {
        shopcarService.deleteShopcarItem(ids);
        return Result.success(null)
                .apply("删除购物车图书项成功");
    }
}
